#include "mlog.h"
//成员函数和构造函数的实现
MLog::MLog(const string& FileName,const int& RollingFileNum,const size_t& fileSize,const int& PRIORITY){
    //1.设置输出器
    RollingFileAppender* term1 = new RollingFileAppender("term1",FileName,fileSize,
                                                         RollingFileNum,true,00644);
    OstreamAppender* term2 =  new OstreamAppender("term2",&cout);//终端输出

    //2.设置布局器
    PatternLayout* layout1 = new PatternLayout();
    PatternLayout* layout2 = new PatternLayout();

    layout1->setConversionPattern("%d: [%p] :%m");//%m
    layout2->setConversionPattern("%d: [%p] :%m");

    //3.添加布局
    term1->setLayout(layout1);
    term2->setLayout(layout2);

    //4.将输出器添加到记录器中
    root.addAppender(term1);
    root.addAppender(term2);

    //5.设置优先级
    root.setPriority(PRIORITY);

    printf("done setting root \n");

}

MLog:: ~MLog(){
    Category::shutdown();
    printf("destructor\n");
}

MLog*  MLog:: logCntl(const bool flag,const string& FileName,
                      const size_t& fileSize ,const int& RollingFileNum ,
                      const int& PRIORITY){

    static MLog* MLogInstance = nullptr;
    if(1 == flag){
        if(nullptr == MLogInstance){
            MLogInstance = new MLog(FileName,RollingFileNum,fileSize,PRIORITY);
            printf("done creating new Mlog\n");
        }else{
            printf("instance Already exist\n");
        }    

    }else{
        if(MLogInstance==nullptr){
            printf("intance already deleted\n");
        }else{
            delete MLogInstance;
        }

    }

    return MLogInstance;
}


string last_msg="";
void MLog:: logInfo(const string& msg)const{
    ostringstream strstream;
    
    //strstream << __FILE__ <<" : "<<__LINE__<<" : "<<__FUNCTION__<<" : "<<msg<<endl;
    if(last_msg!=msg)
    {
        strstream <<msg<<endl;
        root.info(strstream.str());  
    }
    last_msg=msg;  
    /* cout << strstream.str()<<endl; */
}
string last_msg_err="";
void MLog:: logError(const string& msg)const{
    ostringstream strstream;
    //strstream << __FILE__ <<" : "<<__LINE__<<" : "<<__FUNCTION__<<" : "<<msg<<endl;
    if(last_msg_err!=msg)
    	{
	   strstream <<msg<<endl;
    	   root.error(strstream.str());
	}
     last_msg_err=msg;
    /* cout << strstream.str()<<endl; */

}
string last_msg_warn="";
void MLog:: logWarn(const string& msg)const{
    ostringstream strstream;
    //strstream << __FILE__ <<" : "<<__LINE__<<" : "<<__FUNCTION__<<" : "<<msg<<endl;
    if(last_msg_warn!=msg)
    {
       strstream <<msg<<endl;
       root.warn(strstream.str());
    }
    last_msg_warn=msg;
    /* cout << strstream.str()<<endl; */
}
void MLog:: logDebug(const string& msg)const{
    ostringstream strstream;
    strstream <<msg<<endl;
    //strstream << __FILE__ <<" : "<<__LINE__<<" : "<<__FUNCTION__<<" : "<<msg<<endl;
    root.debug(strstream.str());
    /* cout << strstream.str()<<endl; */
}

int  MLog::getPriority()const{
    return root.getPriority();
}
void MLog::setpriority(int PRIORITY){
    root.setPriority(PRIORITY);
}


